/**
 * Created by Tuffy on 16/1/29.
 */
'use strict';

angular.module('starter.controllers')
.controller('DistributionCtrl', ['$scope','$rootScope', '$timeout', '$cordovaActionSheet', '$ionicModal', '$state', 'ReportDistributionService','$cordovaToast', 'ionicMaterialMotion', 'ionicMaterialInk',
    function($scope,$rootScope, $timeout, $cordovaActionSheet, $ionicModal, $state, ReportDistributionService,$cordovaToast, ionicMaterialMotion, ionicMaterialInk) {

    var userid = $rootScope.localStorageObj.userId;


    $scope.distribution = {
        search: {
            value:'',
            filter:{},
            doAct:function(){
                $scope.distribution.page.pageNum = 1;
                if(this.timer){
                    $timeout.cancel(this.timer);
                }
                this.timer = $timeout(function(){
                    $scope.distribution.data = [];
                    $scope.distribution.loadData();
                },500);
            }
        },
        page: {
            pageNum:1,
            pageSize:5,
            total:200
        },
        loaded:true,
        data:[],
        loadData:function(okFunc){
            var _this = this,
                params = {
                    userId:userid,
                    keyLabel:"1",//获取分销数据
                    hotelName:this.search.value,
                    pageNum:_this.page.pageNum,
                    pageSize:_this.page.pageSize
                };
            ReportDistributionService.findDistributionList(params)
                .success(function(res){
                    if(res.success){
                        if(typeof (okFunc) == 'function'){
                            okFunc();
                        }
                        _this.data = _this.data.concat(res.attribute.rows);
                        _this.page.total = res.attribute.total;
                    }else{
                        _this.page.total = 0;
                        console.log('加载失败');
                    }
                }).error(function(error){
                _this.page.total = 0;
                console.log(error);
            }).finally(function(){
                _this.loaded = true;
                $scope.$broadcast('scroll.refreshComplete');
                $scope.$broadcast('scroll.infiniteScrollComplete');
                $timeout(function() {
                    ionicMaterialMotion.fadeSlideIn();
                }, 200);

                $timeout(function() {
                    ionicMaterialInk.displayEffect();
                }, 1000);
            });
        },
        moreDataCanBeLoaded:function(){
            return this.page.total > this.page.pageNum*this.page.pageSize;
        },
        loadMore:function(){
            this.page.pageNum++;
            this.loadData();
        },
        refresh:function(){
            var _this = this;
            _this.page.pageNum = 1;
            _this.loadData(function(){
                _this.data = [];
            });
        }
    } ;

    //  查看详细
    $scope.showDetail = function(id) {
        $state.go('app.chars-distribution-detail', {id: id});
    };

    $scope.distribution.loadData();

}])
.service('ReportDistributionService',['CrmHttp','$rootScope',function(CrmHttp,$rootScope){
    var url = '/crm/crmHotel/';
    return {
        /**
         * 分销酒店全部数据
         * @param params
         * @returns {*}
         */
        findDistributionList: function (params) {
            var act = 'findDistributionList';
            return CrmHttp.post(url + act, params);
        }
    };
}]);